//
//  RegisterViewController.m
//  Homestay
//
//  Created by admin on 2017/3/14.
//  Copyright © 2017年 ysln. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginViewController.h"
#import <UMSocialCore/UMSocialCore.h>
#import "LoginView.h"

@interface RegisterViewController ()

@property (nonatomic , strong)LoginView *Views;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:[self Views]];
}

-(LoginView *)Views{
    
    if (!_Views) {
        
        _Views = [[LoginView alloc] initWithFrame:self.view.frame];
        [_Views.eyeBtn addTarget:self action:@selector(eyeBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_Views.losepsdBtn addTarget:self action:@selector(losePsd:) forControlEvents:UIControlEventTouchUpInside];
        [_Views.loginBtn addTarget:self action:@selector(nextBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_Views.loginBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_Views.wxBtn addTarget:self action:@selector(wxBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_Views.qqBtn addTarget:self action:@selector(qqBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _Views;
}



-(void)tipTestError{
    
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"呃，我们未能识别您输入的验证码";
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.textColor = [UIColor whiteColor];
    tipLabel.font = [UIFont systemFontOfSize:14*kDeviceHRatio];
    tipLabel.backgroundColor = [UIColor colorWithRed:227/255.f green:114/255.f blue:139/255.f alpha:1.0];
    [_Views.background addSubview:tipLabel];
    _tipLabel = tipLabel;
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_Views.background.mas_top).offset(64);
        make.width.mas_equalTo(_Views.background.mas_width);
        make.height.mas_equalTo(30);
    }];
    //推迟3秒执行
    [self performSelector:@selector(deallocTipView) withObject:nil afterDelay:3.0f];
    
}

-(void)deallocTipView{
    
    [UIView animateWithDuration:0.1f animations:^{
       
        _tipLabel.hidden = YES;
        [_tipLabel removeFromSuperview];
        _tipLabel = nil;
    }];
}

#pragma mark-注册完成
-(void)registerFinish:(UIButton *)sender{
    
    [self tipTestError];
    
}
#pragma mark-微信第三方登录
-(void)wxBtn:(UIButton *)sender{
    
    [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];
}


#pragma mark-qq第三方登录
-(void)qqBtn:(UIButton *)sender{
    
    [self getUserInfoForPlatform:UMSocialPlatformType_QQ];
    
}

- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
        
        if (error) {
            
            SLog(@"error:%@",error);
            
        } else {
            
            
            SLog(@"result:%@",result);
            UMSocialUserInfoResponse *resp = result;
            
            // 第三方登录数据(为空表示平台未提供)
            // 授权数据
            SLog(@" uid: %@", resp.uid);
            SLog(@" openid: %@", resp.openid);
            SLog(@" accessToken: %@", resp.accessToken);
            SLog(@" refreshToken: %@", resp.refreshToken);
            SLog(@" expiration: %@", resp.expiration);
            
            // 用户数据
            SLog(@" name: %@", resp.name);
            SLog(@" iconurl: %@", resp.iconurl);
            SLog(@" gender: %@", resp.gender);
            
            // 第三方平台SDK原始数据
            SLog(@" originalResponse: %@", resp.originalResponse);
        }
    }];
}




#pragma mark-下一步
-(void)nextBtn:(UIButton *)sender{
    
    WS(weakself);
    
    [UIView animateWithDuration:0.2f animations:^{
        
        _Views.contextView.hidden = YES;
        [_Views.contextView removeFromSuperview];
        _Views.contextView = nil;
        [sender removeFromSuperview];
        
        
    }];
    
    UIView *testView = [[UIView alloc] init];
    testView.backgroundColor = [UIColor whiteColor];
    testView.layer.cornerRadius = 5.f;
    testView.layer.shadowColor = [UIColor grayColor].CGColor;//shadowColor阴影颜色
    testView.layer.shadowOffset = CGSizeMake(4,4);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    testView.layer.shadowOpacity = 0.8;//阴影透明度，默认0
    testView.layer.shadowRadius = 4;//阴影半径，默认3
    [_Views.background addSubview:testView];
    [testView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakself.view.mas_left).offset(40*kDeviceWRatio);
        make.right.mas_equalTo(weakself.view.mas_right).offset(-40*kDeviceWRatio);
        make.top.mas_equalTo(_Views.titleLabel.mas_bottom).offset(20*kDeviceHRatio);
        make.height.mas_equalTo(205*kDeviceHRatio);
    }];
    
    
    UILabel *title = [[UILabel alloc] init];
    title.font = [UIFont systemFontOfSize:15*kDeviceHRatio];
    title.text = @"我们已将验证码发送至您的手机";
    title.textColor = [UIColor colorWithRed:94/255.f green:94/255.f blue:94/255.f alpha:1.0];
    title.textAlignment = NSTextAlignmentCenter;
    [testView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(testView.mas_top).offset(60*kDeviceHRatio);
        make.width.mas_equalTo(testView.mas_width);
        make.centerX.mas_equalTo(testView.mas_centerX);
    }];
    
    
    UITextField *testTextField = [[UITextField alloc] init];
    testTextField.placeholder = @"输入验证码";
    testTextField.font = [UIFont systemFontOfSize:14*kDeviceHRatio];
    [testView addSubview:testTextField];
    [testTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(testView.mas_left).offset(22.5*kDeviceWRatio);
        make.right.mas_equalTo(testView.mas_right).offset(-22.5*kDeviceWRatio);
        make.top.mas_equalTo(title.mas_bottom).offset(35*kDeviceHRatio);
    }];

    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor blackColor];
    [testView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(testView.mas_left).offset(22.5*kDeviceWRatio);
        make.top.mas_equalTo(testTextField.mas_bottom).offset(5*kDeviceHRatio);
        make.right.mas_equalTo(testView.mas_right).offset(-22.5*kDeviceWRatio);
        make.height.mas_equalTo(1*kDeviceHRatio);
    }];
    
    UIButton *resetTest = [[UIButton alloc] init];
    [resetTest setTitle:@"发送验证码" forState:UIControlStateNormal];
    [resetTest setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    resetTest.titleLabel.font = [UIFont systemFontOfSize:13*kDeviceHRatio];
    [resetTest addTarget:self action:@selector(resetTest:) forControlEvents:UIControlEventTouchUpInside];
    resetTest.backgroundColor = [UIColor colorWithRed:119/255.f green:180/255.f blue:247/255.f alpha:1.0];
    resetTest.layer.cornerRadius = 5.f;
    [testView addSubview:resetTest];
    _resetTest = resetTest;
    [resetTest mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(line.mas_right).offset(-5*kDeviceWRatio);
        make.bottom.mas_equalTo(line.mas_bottom).offset(-5*kDeviceHRatio);
        make.width.mas_equalTo(90*kDeviceWRatio);
        make.height.mas_equalTo(25*kDeviceHRatio);
    }];
    
    UIButton *registerBtn = [[UIButton alloc] init];
    registerBtn.layer.cornerRadius = 20.f;
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerBtn.backgroundColor = [UIColor colorWithRed:119/255.f green:180/255.f blue:247/255.f alpha:1.0];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:16*kDeviceHRatio];
    [registerBtn addTarget:self action:@selector(registerFinish:) forControlEvents:UIControlEventTouchUpInside];
    [_Views.background addSubview:registerBtn];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(testView.mas_centerX);
        make.top.mas_equalTo(testView.mas_bottom).offset(-17.5*kDeviceHRatio);
        make.width.mas_equalTo(125*kDeviceWRatio);
        make.height.mas_equalTo(35*kDeviceHRatio);
    }];
    
    [self resetTest:nil];//点击下一步默认已发送一次验证码
    
}

- (void)updateTimer:(NSTimer *)timer
{
    //    int counter = self.vcodeTime;
    
    if (--self.vcodeTime < 0) {
        // 停止时钟
        [self pause];
        [_resetTest setTitle:@"发送验证码" forState:UIControlStateNormal];
        [_resetTest setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _resetTest.enabled = YES;
    } else {
        
        NSString *btnTitle = [NSString stringWithFormat:@"%d秒后重发", self.vcodeTime];
        [_resetTest setTitle:btnTitle forState:UIControlStateNormal];
        
        [_resetTest setTitleColor:kTextGaryColor forState:UIControlStateNormal];
        _resetTest.enabled = NO;
    }
}

/** 暂停 */
- (void)pause
{
    [self.timer invalidate];
}


#pragma mark-重新发送验证码
-(void)resetTest:(UIButton *)sender{
    
    self.vcodeTime = 60;
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
    // 将timer添加到运行循环
    // 模式：NSRunLoopCommonModes的运行循环模式（监听滚动模式）
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

#pragma mark-忘记密码
-(void)losePsd:(UIButton *)sender{
    
    
}


#pragma mark-显示密码
-(void)eyeBtn:(UIButton *)sender{
    
    sender.selected=!sender.selected;
    if(sender.selected){
        
        //        //避免明文/密文切换后光标位置偏移
        //        self.psdTextField.enabled = NO;    // the first one;
        _Views.psdTextField.secureTextEntry = NO;
        //        sender.selected = !sender.selected;
        //        self.psdTextField.enabled = YES;  // the second one;
        [_Views.psdTextField becomeFirstResponder];
        
    }else{
        
        _Views.psdTextField.secureTextEntry = YES;
        [_Views.psdTextField becomeFirstResponder];
        
    }
}

- (void)viewWillAppear:(BOOL)animated{
    
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(PushLogin:)];
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backVC:)];
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.rightBarButtonItem = rightBtn;
    leftBtn.tintColor = [UIColor whiteColor];
    rightBtn.tintColor = [UIColor whiteColor];
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

#pragma mark-返回上一级
-(void)backVC:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark-注册
-(void)PushLogin:(UIButton *)sender{
    
    LoginViewController *vc = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
